package com.anywhere.freightman.car.service.impl;

import com.anywhere.freightman.car.dao.CarDao;
import com.anywhere.freightman.car.model.CarModel;
import com.anywhere.freightman.car.model.DriverModel;
import com.anywhere.freightman.car.service.ICarService;
import com.anywhere.freightman.common.BaseEntityService;
import com.anywhere.freightman.common.BaseMapper;
import com.anywhere.freightman.common.entity.car.CarQueryEntity;
import com.anywhere.freightman.common.ex.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

@Service
@Transactional
public class CarService extends BaseEntityService<CarModel, CarQueryEntity> implements ICarService {

    @Autowired
    private CarDao carDao;

    @Override
    public BaseMapper<CarModel> getBaseDao() {
        return carDao;
    }

    @Override
    public Example getExample(CarQueryEntity entity) {
        Weekend<CarModel> weekend = Weekend.of(CarModel.class);
        WeekendCriteria<CarModel, Object> criteria = weekend.weekendCriteria();

        if (entity == null) {
            return weekend;
        }

        if (entity.getCompanyId() != null && entity.getCompanyId() > 0) {
            criteria.andEqualTo("companyId", entity.getCompanyId());
        }

        if (!StringUtils.isEmpty(entity.getCarNumber())) {
            criteria.andLike("carNumber", "%" + entity.getCarNumber() + "%");
        }

        return weekend;
    }

    @Override
    public CarModel getByCarNumber(String carNumber) {
        return carDao.getByCarNumber(carNumber);
    }

    @Override
    public List<DriverModel> getDriverList(Long carId) {
        return carDao.getDriverList(carId);
    }

    @Override
    public List<CarModel> getListByCompanyId(Long companyId) {
        return carDao.getListByCompanyId(companyId);
    }

    @Override
    public List<CarModel> getTrailerListByCompanyId(Long companyId) {
        return carDao.getTrailerListByCompanyId(companyId);
    }

    @Override
    public void bindDriver(long carId, long driverId) {
        List<DriverModel> driverList = getDriverList(carId);
        boolean isBlank = true;
        for (DriverModel item : driverList) {
            if (driverId == item.getId().longValue()) {
                isBlank = false;
                break;
            }
        }
        if (isBlank) {
            carDao.bindDriver(carId, driverId);
        }

    }

    @Override
    public void removeDriver(long carId) {
        carDao.removeAllDriver(carId);
    }

    @Override
    public void removeDriver(long carId, long driverId) {
        carDao.removeDriver(carId, driverId);
    }

    @Override
    public void removeDriverByDriver(long driverId) {
        carDao.removeDriverByDriverId(driverId);
    }

    @Override
    public void remove(long carId) {
        CarModel carModel = getById(carId);
        if (carModel != null) {
            removeDriver(carModel.getId());
            delete(carModel);
        } else {
            throw new CustomException("车辆信息不存在");
        }
    }

    @Override
    public void insert(CarModel model) {
        CarModel m = getByCarNumber(model.getCarNumber());
        if (m != null) {
            throw new CustomException("车牌号已经存在");
        }

        super.insert(model);
    }


}
